<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\UserTag;
use Illuminate\Http\Request;

class UserTagController extends BaseController
{

    protected $model = UserTag::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $input = $request->input();
        $search = ['title' => ''];
        $list = UserTag::apply($search, $input)->orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($list);
    }

    public function store(Request $request)
    {
        $input = $request->input();
        $id = \request("id");
        $info = UserTag::where('title', $input['title'])->first();
        if ($id) {
            $one = $this->model::find($id);
            if (!$one) {
                return $this->failed('ID不存在');
            }
            if ($info && $info->id != $id)
                return $this->failed('名称已经存在');

            $one->update($input);
        } else {
            if ($info)
                return $this->failed('名称已经存在');

            $info = $this->model::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

}

